package net.tv90.appserveradmin.satoken;

import cn.dev33.satoken.stp.StpInterface;
import net.tv90.appserveradmin.model.Users;
import net.tv90.appserveradmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component    // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    static List<String> adminRoleList = new ArrayList<>(Arrays.asList("admin", "doctor"));
    static List<String> doctorRoleList = new ArrayList<>(List.of("doctor"));
    static List<String> nullRoleList = new ArrayList<>(List.of());

    @Autowired
    UserService userService;

    @Override
    public List<String> getPermissionList(Object o, String s) {
        return new ArrayList<>();
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        Users user = userService.getById((Serializable) loginId);
        if (user == null || user.getUserType() == null) {
            return nullRoleList;
        }
        return switch (user.getUserType()) {
            case 2 -> doctorRoleList;
            case 3 -> adminRoleList;
            default -> nullRoleList;
        };
    }
}